package com.zk.algorithm;

import com.zk.algorithm.bean.Element;
import com.zk.algorithm.bean.ListNode;
import com.zk.algorithm.bean.TreeNode;

import java.util.*;

public class Utils {

    public static int max(int...vals) {
        return Arrays.stream(vals).max().getAsInt();
    }

    public static void println(int val) {
        System.out.println(val);
    }

    public static void println(boolean val) {
        System.out.println(val);
    }

    public static final void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    public static final void println(int[] array) {
        println(array, 0, array.length - 1);
    }

    public static final void println(int[] array, int begin, int end) {
        StringBuilder stringBuilder = new StringBuilder(array.length * 3 + 2);
        stringBuilder.append("[");

        for (int i = begin; i <= end; i++) {
            stringBuilder.append(array[i]);

            if (i != array.length - 1) { // not last one
                stringBuilder.append(",");
            }
        }

        stringBuilder.append("]");

        System.out.println(stringBuilder.toString());
    }

    public static final void println(ListNode head) {
        List<Element> elements = new ArrayList<>();

        while (head != null) {
            elements.add(head);
            head = head.getNext();
        }

        println(elements);
    }

    public static final void println(String str) {
        System.out.println(str);
    }

    public static final void println(List<? extends Element> elements) {
        println(elements, 0, elements.size() - 1);
    }

    public static final void println(List<? extends Element> elements, int begin, int end) {
        StringBuilder stringBuilder = new StringBuilder(elements.size() * 3 + 2);
        stringBuilder.append("[");

        for (int i = begin; i <= end; i++) {
            int val = elements.get(i).getVal();
            stringBuilder.append(val != Integer.MIN_VALUE ? val : "null");

            if (i != elements.size() - 1) { // not last one

                String separator = ",";
                if (elements.get(i) instanceof ListNode) {
                    separator = "->";
                }

                stringBuilder.append(separator);
            }
        }

        stringBuilder.append("]");

        System.out.println(stringBuilder.toString());
    }

    public static final void println(TreeNode root) {
        List<TreeNode> list = new LinkedList<>();

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            list.add(cur);

            if (cur.left != null) {
                queue.add(cur.left);
            }

            if (cur.right != null) {
                queue.add(cur.right);
            }
        }

        println(list);
    }

    public static final void println(int[][] array) {
        System.out.println(Arrays.deepToString(array).replace("], ", "]\n").replace("[[", "[").replace("]]", "]"));
    }

    public static final void println(Object object) {
        System.out.println(String.valueOf(object));
    }

    public static final void fill(int[][] array, int val) {
        for (int i = 0; i < array.length; i++) {
            Arrays.fill(array[i], val);
        }
    }

}
